package cn.edu.ujn.bmis.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import cn.edu.ujn.bmis.model.SysFunction;
import cn.edu.ujn.bmis.model.UserInfo;
import cn.edu.ujn.bmis.service.SysFunctionManager;
import cn.edu.ujn.bmis.service.UserInfoManager;
import cn.edu.ujn.bmis.tools.Helper;

import com.opensymphony.xwork2.ActionSupport;

@Component("SysFunction")
@Scope("prototype")
public class SysFunctionAction extends ActionSupport {

	private static final long serialVersionUID = -6866244164477029741L;
	private SysFunctionManager sysFunctionManager;
	private List<SysFunction> sysFunctionList = new ArrayList<SysFunction>();
	private SysFunction sysFunction;
	private String userId;
	private String targetType = "navTab"; // navTab或dialog，用来标记是navTab上的分页还是dialog上的分页
	private int pageNum = 1;
	private int currentPage = 1; // 当前是第几页
	private int totalCount = 0; // 总条数
	private int numPerPage = 5; // 每页显示多少条
	private int pageNumShown = 5; // 页标数字多少个
	private int pageCount = 0; // 总页数
	private String orderField;
	private String orderDirection;
	private Helper helper;
	private String status;
	private UserInfoManager userInfoManager;
	private String functionNum;
	private String functionName;
	
	/**
	 * 查看系统功能
	 */
	@SuppressWarnings("unchecked")
	public String findSysFunction() {
		//功能权限验证
		if(!helper.checkFunction(functionNum)){
			Helper.out("403", "该用户没有此功能权限!", "", "", "", "");
		}
		String hql = "";
		String hql_size = "";
		if (userId == null) {
			userId = "";
		}
		// 检查输入的读者编号是否存在
		String hql_check = "";
		List<UserInfo> userInfoList = new ArrayList<UserInfo>();
		if (userId != null && !"".equals(userId)) {
			hql_check = "from UserInfo u where u.userId='" + userId.trim()
					+ "'";
			userInfoList = userInfoManager.findUserInfo(hql_check);
			if (userInfoList.size() <= 0) {
				Helper.out("300", "输入的用户编号不存在请检查！", "", "", "", "");
			}else{
				status=userInfoList.get(0).getUserStatus();
			}
			
		}
		this.currentPage = this.pageNum;
		hql = "select s from SysFunction s where not exists("
				+ "select 1 from UserFunction u where s.functionNum=u.sysFunction.functionNum "
				+ "and u.userInfo.userId='" + userId.trim() + "')";
		hql_size = "select count(*) from SysFunction s where not exists("
				+ "select 1 from UserFunction u where s.functionNum=u.sysFunction.functionNum "
				+ "and u.userInfo.userId='" + userId.trim() + "')";
		if(functionName!=null&&!"".equals(functionName.trim())){
			hql+=" and s.functionName like '%"+functionName.trim()+"%' ";
			hql_size+=" and s.functionName like '%"+functionName.trim()+"%' ";
		}
		if("读者".equals(status)||status==null){
			hql+=" and s.functionNum like 'hxlreader%'";
			hql_size+=" and s.functionNum like 'hxlreader%'";
		}else if("图书管理员".equals(status)){
			hql+=" and s.functionNum like 'hxlbookad%'";
			hql_size+=" and s.functionNum like 'hxlbookad%'";
		}else if("系统管理员".equals(status)){
			hql+=" and s.functionNum like 'hxlsysad%'";
			hql_size+=" and s.functionNum like 'hxlsysad%'";
		}
		totalCount = helper.findSize(hql_size);
		pageCount = totalCount % numPerPage == 0 ? totalCount / numPerPage
				: totalCount / numPerPage + 1;
		sysFunctionList = helper.fenYe(hql, this.currentPage, this.numPerPage);
		return SUCCESS;
	}

	public SysFunctionManager getSysFunctionManager() {
		return sysFunctionManager;
	}

	@Resource(name = "sysFunctionManager")
	public void setSysFunctionManager(SysFunctionManager sysFunctionManager) {
		this.sysFunctionManager = sysFunctionManager;
	}

	public SysFunction getSysFunction() {
		return sysFunction;
	}

	public void setSysFunction(SysFunction sysFunction) {
		this.sysFunction = sysFunction;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getTargetType() {
		return targetType;
	}

	public void setTargetType(String targetType) {
		this.targetType = targetType;
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public int getNumPerPage() {
		return numPerPage;
	}

	public void setNumPerPage(int numPerPage) {
		this.numPerPage = numPerPage;
	}

	public int getPageNumShown() {
		return pageNumShown;
	}

	public void setPageNumShown(int pageNumShown) {
		this.pageNumShown = pageNumShown;
	}

	public int getPageCount() {
		return pageCount;
	}

	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}

	public String getOrderField() {
		return orderField;
	}

	public void setOrderField(String orderField) {
		this.orderField = orderField;
	}

	public String getOrderDirection() {
		return orderDirection;
	}

	public void setOrderDirection(String orderDirection) {
		this.orderDirection = orderDirection;
	}

	public Helper getHelper() {
		return helper;
	}

	@Resource(name = "helper")
	public void setHelper(Helper helper) {
		this.helper = helper;
	}

	public List<SysFunction> getSysFunctionList() {
		return sysFunctionList;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public UserInfoManager getUserInfoManager() {
		return userInfoManager;
	}
	
	@Resource(name="userInfoManager")
	public void setUserInfoManager(UserInfoManager userInfoManager) {
		this.userInfoManager = userInfoManager;
	}

	public String getFunctionNum() {
		return functionNum;
	}

	public void setFunctionNum(String functionNum) {
		this.functionNum = functionNum;
	}

	public String getFunctionName() {
		return functionName;
	}

	public void setFunctionName(String functionName) {
		this.functionName = functionName;
	}
	
}
